﻿namespace MyWebLog.Tests.Logic.Models
{
    using DatabaseAbstraction.Utils.UnitTest;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using MyWebLog.Logic.Models;
    using System.Data;

    /// <summary>
    /// Unit tests for the Blog class
    /// </summary>
    [TestClass]
    public class BlogTest
    {
        /// <summary>
        /// Test the empty constructor
        /// </summary>
        [TestMethod]
        public void EmptyConstructor()
        {
            var blog = new Blog();
            Assert.IsNotNull(blog, "The blog object should not be null");
        }

        /// <summary>
        /// Test the data constructor
        /// </summary>
        [TestMethod]
        public void DataConstructor()
        {
            Blog blog;

            using (var reader = GetDataReader())
                blog = new Blog(reader);

            Assert.IsNotNull(blog, "The blog object should not be null");
            Assert.AreEqual(15, blog.ID, "ID not filled correctly");
            Assert.AreEqual("This Blog", blog.Name, "Name not filled correctly");
            Assert.AreEqual("A really cool blog", blog.Subtitle, "Subtitle not filled correctly");
            Assert.AreEqual("http://localhost/cool/blog", blog.BaseURL, "Base URL not filled correctly");
        }

        /// <summary>
        /// Test the data parameters
        /// </summary>
        [TestMethod]
        public void Parameters()
        {
            Blog blog;

            using (var reader = GetDataReader())
                blog = new Blog(reader);

            var parameters = blog.Parameters();
            Assert.IsNotNull(parameters);

            Assert.IsTrue(parameters.ContainsKey("blog_id"), "Blog ID key not found in parameter dictionary");
            Assert.IsTrue(parameters.ContainsKey("blog_name"), "Blog Name key not found in parameter dictionary");
            Assert.IsTrue(parameters.ContainsKey("subtitle"), "Subtitle key not found in parameter dictionary");
            Assert.IsTrue(parameters.ContainsKey("base_url"), "Base URL key not found in parameter dictionary");

            Assert.AreEqual(15, parameters["blog_id"], "ID not correct");
            Assert.AreEqual("This Blog", parameters["blog_name"], "Name not correct");
            Assert.AreEqual("A really cool blog", parameters["subtitle"], "Subtitle not correct");
            Assert.AreEqual("http://localhost/cool/blog", parameters["base_url"], "Base URL not correct");
        }

        /// <summary>
        /// Get a reader with test data, pointing to a row of test data
        /// </summary>
        /// <returns>
        /// A ready-to-use data reader
        /// </returns>
        private IDataReader GetDataReader()
        {
            var results = new StubResultSet("blog_id", "blog_name", "subtitle", "base_url");

            results.AddRow(15, "This Blog", "A really cool blog", "http://localhost/cool/blog");

            var reader = new StubDataReader(results);
            reader.Read();

            return reader;
        }
    }
}